var express = require('express');
var md5 = require('md5');
var request = require('request');
var consts = require('../common/constants');

var router = express.Router();

//退货订单列表
router.get('/list', function(req, res, next) {
    var params = {
        userId: req.query.uid,
        pageSize: consts.PageSize,
        pageNum: req.query.num
    };
    var uid= req.query.uid;
    var token = JSON.stringify(params) + "|" + consts.MD5_String;
    token = md5(token);
    var url = consts.APP_HOST + 'serverorder/list.htm?token=' + token;
    var options = {
        url: url,
        method: 'POST',
        json: true,
        body: params
    };

    request(options, function(err, httpResponse, body) {
        if (1 == body.MobileHead.Code) {
            //res.send(body.MobileBody);
            var total = body.MobileBody.pageCount;
            var totalPage = parseInt(total/consts.orderPageSize);
            if (total%consts.orderPageSize>0) {
               totalPage += 1;
            }
            res.render('orderRetrun', {

                 orderList: body.MobileBody,
                 orderListJ: JSON.stringify(body.MobileBody),
                 totalPage: totalPage,
                 uid: uid,
                 column: 'list'
             })
        } else {
            res.send('404');
        }
    });
});

//退货订单详情
router.get('/detail', function(req, res, next) {
    var params = {
        userId: req.query.uid,
        orderId: req.query.oid
    };

    var uid=req.query.uid;
    var token = JSON.stringify(params) + "|" + consts.MD5_String;
    token = md5(token);
    var url = consts.APP_HOST + 'serverorder/detail.htm?token=' + token;
    var options = {
        url: url,
        method: 'POST',
        json: true,
        body: params
    };

    request(options, function(err, httpResponse, body) {
        console.log(body);
        if (1 == body.MobileHead.Code) {

            res.render('memberServiceOrderDeatil', {

                 serviceOrderDeatil: body.MobileBody,
                 //orderListJ: JSON.stringify(body.MobileBody),
                  uid: uid
             })


        } else {
            res.send('404');
        }
    });
});

//退货单进度查询
router.get('/logs', function(req, res, next) {
    //type扩展类型，默认为0退货理由
    var params = {
        userId: req.query.uid,
        orderId: req.query.oid
    };

    var token = JSON.stringify(params) + "|" + consts.MD5_String;
    token = md5(token);
    var url = consts.APP_HOST + 'serverorder/logs.htm?token=' + token;

    var options = {
        url: url,
        method: 'POST',
        json: true,
        body: params
    };
    request(options, function(err, httpResponse, body) {
        console.log(body);
        if (1 == body.MobileHead.Code) {
            res.send(body.MobileBody);
        } else {
            res.send('404');
        }
    });
});



module.exports = router;